unit uDmConexao;

interface

uses
  uConexao, Forms, StdCtrls, ComCtrls,
  SysUtils, Classes, ZAbstractConnection, ZConnection, DB,
  ZAbstractRODataset, ZAbstractDataset, ZDataset, DAScript, UniScript,
  DBAccess, Uni, UniProvider, PostgreSQLUniProvider, MemDS;

type
  TdmConexao = class(TDataModule)
    UniConnection: TUniConnection;
    PostgreSQLUniProvider: TPostgreSQLUniProvider;
  private
    { Private declarations }
    function configuraConnection(const conexao: TConexao): integer;

  public
    { Public declarations }
    function conectarBancoDeDados(const conexao: TConexao): integer;

  end;

var
  dmConexao: TdmConexao;

implementation

uses uConstantesSistema, uMensagemConstante, Dialogs,
  uMensagensSistema, uFuncoes;

{$R *.dfm}

{ TdmConexao }


function TdmConexao.conectarBancoDeDados(const conexao: TConexao): integer;
begin
   try
      result := retOK;
      if UniConnection.Connected then
       UniConnection.Disconnect;
      UniConnection.Username := conexao.usuario;
      UniConnection.Password := conexao.senha;
      UniConnection.Port     := conexao.porta;
      UniConnection.Server   := conexao.servidor;
      UniConnection.Database := conexao.banco;
   except on e: exception do
    begin
       result := retErro;
       mensagemErro(MsgNaoConectouBD);
    end;
   end;
end;

function TdmConexao.configuraConnection(const conexao: TConexao): integer;
begin
   try
      result                 := retOK;
      UniConnection.Username := conexao.usuario;
      UniConnection.Password := conexao.senha;
      UniConnection.Port     := conexao.porta;
      UniConnection.Server   := conexao.servidor;
      UniConnection.Database := '';
      UniConnection.Connect;
   except on e: exception do
    begin
       result := retErro;
       mensagemErro(MsgNaoConectouBD);
       UniConnection.Disconnect;
    end;
   end;
end;

end.
